
#include "bombskyfall.h"
#include "resourcemanager.h"
#include "gamemanager.h"
#include "frameanimation.h"
#include "CCSkeletalSprite.h"
#include "gamedatamanager.h"
#include "army.h"
#include "unit.h"
#include "gamescene.h"

static const float SKY_FALL_TIME = 0.5f;

#define __super         SkyFallBullet

BombSkyFallBullet::BombSkyFallBullet(
		Army *targetArmy,
		EquipmentAttribute *attr,
		GameScene *scene):
__super(targetArmy, attr, scene)
, _delay(0.0f)
{
	_damageType = GDM->getWeaponDamageType(bulletAttribute->projectileType);
	_delay = SKY_FALL_TIME;
}

BombSkyFallBullet::~BombSkyFallBullet() {
}

void BombSkyFallBullet::init() {
	float dx = GM->minscale * 100.0f;
	float dy = GM->minscale * 300.0f;

	velocity = ccp(dx / SKY_FALL_TIME, dy / SKY_FALL_TIME);
	float rotation = -180.0f * ccpToAngle(velocity) / static_cast<float>(M_PI);

	for(int i = 0; i < SQUAD_COUNT; i++) {
		for(int j = 0; j < ROWS_PER_LINE; j++) {
			if(j % 2 == 0) {
				continue;
			}

			float x = i * (GRID_WIDTH * 3 + SQUAD_SPACE) + GRID_WIDTH * 1.5f + FORMATION_MARGIN;
			float y = j * GRID_HEIGHT + FORMATION_MARGIN;

			auto fx = RM->createAnimation("fx_fire_ball", ccp(x-dx, y+dy));
			fx->setRotation(rotation);

			_managedGameNodeInfo.node = fx;
			_gameNodeContainer->addOneTimeNode(_managedGameNodeInfo);
		}
	}
}

void BombSkyFallBullet::createHitEffect() {
	for(int i = 0; i < SQUAD_COUNT; i++) {
		for(int j = 0; j < ROWS_PER_LINE; j++) {
			if(j % 2 == 0) {
				continue;
			}

			float x = i * (GRID_WIDTH * 3 + SQUAD_SPACE) + GRID_WIDTH * 1.5f + FORMATION_MARGIN;
			float y = j * GRID_HEIGHT + FORMATION_MARGIN;

			FrameAnimation *fxHit = RM->createAnimation("fx_fire_bomb_hit", ccp(x, y));
			fxHit->setLoop(false);
			fxHit->setAnchorPoint(ccp(0.5f, 0.0f));
			fxHit->setZOrder(static_cast<int>(GM->screenSize.height - y));

			_managedGameNodeInfo.node = fxHit;
			_gameNodeContainer->addOneTimeNode(_managedGameNodeInfo);
		}
	}
}

void BombSkyFallBullet::updateBolt(float ticks) {
	_delay -= ticks;
}

bool BombSkyFallBullet::isHitTarget() {
	return _delay <= 0.0f;
}
